#include <iostream>
#include <vector>
using namespace std;
const int N = 1007, INF = 0x3f3f3f3f;
int f[N][N], g[N][N];
// f[i][j]：第i天完成j次交易且持有股票的最大收益
// g[i][j]：第i天完成j次交易且不持有股票的最大收益
int main()
{
    int n = 0, k = 0;
    cin >> n >> k;
    k = min(k, n / 2);
    vector<int> arr(n);
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i];
    }
    f[0][0] = -arr[0];
    for(int j = 1; j <= k; ++j)
    {
        f[0][j] = -INF;
        g[0][j] = -INF;
    }

    for(int i = 1; i < n; ++i)
    {
        for(int j = 0; j <= k; ++j)
        {
            f[i][j] = max(f[i - 1][j], g[i - 1][j] - arr[i]);
            g[i][j] = g[i - 1][j];
            if(j - 1 >= 0)
                g[i][j] = max(g[i][j], f[i - 1][j - 1] + arr[i]);
        }
    }
    int res = 0;
    for(int j = 0; j <= k; ++j)
    {
        res = max(res, g[n - 1][j]);
    }
    cout << res << endl;
    return 0;
}